﻿using MS_Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace common
{
    public static class MyExtensions
    {
        public static XmlDocument WriteVariable(this Comon str,  dynamic VarList)
        {
            XmlDocument rootxml = new XmlDocument();
            var root = rootxml.CreateElement("Root");
            foreach (var item in VarList)
            {
                XmlElement xmle = rootxml.CreateElement("Variable");
                //
                Tools.XMLAttributeAppend(rootxml, "VarName", item.VarName, xmle);
                Tools.XMLAttributeAppend(rootxml, "Address", item.Address, xmle);
                Tools.XMLAttributeAppend(rootxml, "DataType", item.DataType, xmle);
                Tools.XMLAttributeAppend(rootxml, "StoreArea", item.StoreArea, xmle);
                Tools.XMLAttributeAppend(rootxml, "Note", item.Note, xmle);
                Tools.XMLAttributeAppend(rootxml, "IsFilling", item.IsFiling + "", xmle);
                Tools.XMLAttributeAppend(rootxml, "IsAlarm", item.IsAlarm + "", xmle);
                Tools.XMLAttributeAppend(rootxml, "IsReport", item.IsReport, xmle);
                Tools.XMLAttributeAppend(rootxml, "AbsoluteAddress", item.AbsoluteAddress, xmle);
                root.AppendChild(xmle);
            }
            rootxml.AppendChild(root);
            return rootxml;
        }
        public static XmlDocument WriteStoreArea(this Comon str, dynamic VarList)
        {
            XmlDocument rootxml = new XmlDocument();
            var root = rootxml.CreateElement("Root");
            foreach (StoreArea item  in VarList)
            {
           
                XmlElement xmle = rootxml.CreateElement("StoreArea");
                //
                Tools.XMLAttributeAppend(rootxml, "StoreTyle", item.StoreType, xmle);
                Tools.XMLAttributeAppend(rootxml, "StartReg", item.StartReg + "", xmle);
                Tools.XMLAttributeAppend(rootxml, "Length", item.Length + "", xmle);
             
                root.AppendChild(xmle);
            }
            rootxml.AppendChild(root);
            return rootxml;
        }
        public static XmlDocument WriteVarAlarm(this Comon str, dynamic VarList)
        {
            XmlDocument rootxml = new XmlDocument();
            var root = rootxml.CreateElement("Root");
            foreach (VarAlarm_Modbus item in VarList)
            {
                XmlElement xmle = rootxml.CreateElement("Variable");
                //
                Tools.XMLAttributeAppend(rootxml, "VarName", item.VarName, xmle);
                string alarm = string.Empty;
                foreach(var i in item.VarAlarm)
                {
                    alarm += i.AlarmType + " ; " + i.AlarmValue + " ; " +
                        i.Priority + " ; " + i.Note + " | ";
                }
                Tools.XMLAttributeAppend(rootxml, "Alarm", alarm, xmle);
                root.AppendChild(xmle);
            }
            rootxml.AppendChild(root);
            return rootxml;
        }
        public static List<Variable_Modus> GetVariable (this Comon str, XmlDocument xd)
        {
            List<Variable_Modus> res = new List<Variable_Modus>();
            foreach (XmlNode i in xd)
            {
                if (i.Name == "Root")
                {
                    foreach (XmlNode j in i.ChildNodes)
                    {
                        if (j.Name == "Variable")
                        {

                            Variable_Modus vm = new Variable_Modus();
                            vm.VarName = Tools.GetXMLValue(j, "VarName");
                            vm.Address = Tools.GetXMLValue(j, "Address");
                            vm.Note = Tools.GetXMLValue(j, "Note");
                            vm.AbsoluteAddress = Tools.GetXMLValue(j, "AbsoluteAddress");
                            vm.IsReport = Tools.GetXMLValue(j, "IsReport");
                            vm.DataType = Tools.GetXMLValue(j, "DataType");
                            vm.IsAlarm = Convert.ToBoolean(Tools.GetXMLValue(j, "IsAlarm"));
                            vm.StoreArea = Tools.GetXMLValue(j, "StoreArea");
                            res.Add(vm);
                        }
                    }
                }
            }
            return res;
        }
        public static List<VarAlarm_Modbus> GetVarAlarm(this Comon str, XmlDocument xd)
        {
            List<VarAlarm_Modbus> res = new List<VarAlarm_Modbus>();
            var c = new Comon();
            foreach (XmlNode i in xd)
            {
                if (i.Name == "Root")
                {
                    foreach (XmlNode j in i.ChildNodes)
                    {
                        if (j.Name == "Variable")
                        {

                            VarAlarm_Modbus vm = new VarAlarm_Modbus();
                            vm.VarName = Tools.GetXMLValue(j, "VarName");
                            vm.VarAlarm = c.GetAlarmsParams( Tools.GetXMLValue(j, "Alarm"));

                            res.Add(vm);
                        }
                    }
                }
            }
            return res;
        }
    }
}
